package com.kevis.zhunblog.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kevis.zhunblog.core.dto.PageDomain;
import com.kevis.zhunblog.core.entities.PowerMenuEntity;
import com.kevis.zhunblog.core.entities.RoleEntity;
import com.kevis.zhunblog.core.entities.RolePowerEntity;
import com.kevis.zhunblog.mapper.AdminMapper;
import com.kevis.zhunblog.mapper.PowerMenuMapper;
import com.kevis.zhunblog.mapper.RoleMapper;
import com.kevis.zhunblog.mapper.RolePowerMapper;
import com.kevis.zhunblog.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity>
        implements RoleService {

    /**
     * 注入角色服务
     */
    @Resource
    private RoleMapper sysRoleMapper;

    @Resource
    private RolePowerMapper rolePowerMapper;

    @Resource
    private AdminMapper adminMapper;

    @Resource
    private PowerMenuMapper powerMenuMapper;

    /**
     * Describe: 查询角色数据
     * Param: QueryRoleParam
     * Return: 操作结果
     */
    @Override
    public List<RoleEntity> list(RoleEntity param) {
        LambdaQueryChainWrapper<RoleEntity> wrapper = lambdaQuery();
        if (param != null) {
            if (StringUtils.hasLength(param.getRoleCode())) {
                wrapper.eq(RoleEntity::getRoleCode, param.getRoleCode());
            }
            if (StringUtils.hasLength(param.getRoleName())) {
                wrapper.eq(RoleEntity::getRoleName, param.getRoleName());
            }
        }
        return wrapper.list();
        //return sysRoleMapper.selectRole(param);
    }

    /**
     * Describe: 查询角色数据 分1页
     * Param: QueryRoleParam
     * Return: 操作结果
     */
    @Override
    public PageInfo<RoleEntity> page(RoleEntity param, PageDomain pageDomain) {
        PageHelper.startPage(pageDomain.getPage(), pageDomain.getLimit());
        //List<RoleEntity> list = sysRoleMapper.selectRole(param);
        List<RoleEntity> list = list(param);
        return new PageInfo<>(list);
    }

    /**
     * Describe: 保存角色数据
     * Param: SysRole
     * Return: 操作结果
     */
    @Override
    public boolean save(RoleEntity sysRole) {
        int result = sysRoleMapper.insert(sysRole);
        return result > 0;
    }

    /**
     * Describe: 根据 ID 查询角色
     * Param: id
     * Return: 返回角色信息
     */
    @Override
    public RoleEntity getById(String id) {
        return sysRoleMapper.selectById(id);
    }

    /**
     * Describe: 修改用户数据
     * Param: SysUser
     * Return: 操作结果
     */
    @Override
    public boolean update(RoleEntity sysRole) {
        Integer result = sysRoleMapper.updateById(sysRole);
        return result > 0;
    }

    /**
     * Describe: 根据 id 删除角色数据
     * Param: id
     * Return: Boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean remove(Integer id) {
        sysRoleMapper.deleteById(id);
        adminMapper.deleteByRoleId(id);
        rolePowerMapper.deleteByRoleId(id);
        return true;
    }

    /**
     * 查询当前角色对应菜单权限,并返回菜单及菜单里打钩的结构
     * @param roleId
     * @return
     */
    @Override
    public List<PowerMenuEntity> getRolePower(Integer roleId) {
        List<PowerMenuEntity> powerMenuEntities = powerMenuMapper.selectList(null);
        List<RolePowerEntity> rolePowerEntities = rolePowerMapper.selectByRoleId(roleId);
        powerMenuEntities.forEach(powerMenu -> rolePowerEntities.forEach(rolePowerEntity -> {
            if (rolePowerEntity.getPowerId().equals(powerMenu.getId())) {
                powerMenu.setCheckArr("1");
            }
        }));
        return powerMenuEntities;
    }

    /**
     * Describe: 保存角色权限数据
     * Param: roleId powerIds
     * Return: 执行结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveRolePower(Integer roleId, List<String> powerIds) {
        rolePowerMapper.deleteByRoleId(roleId);
        List<RolePowerEntity> rolePowers = new ArrayList<>();
        powerIds.forEach(powerId -> {
            RolePowerEntity sysRolePower = new RolePowerEntity();
            sysRolePower.setRoleId(roleId);
            sysRolePower.setPowerId(Integer.valueOf(powerId));
            rolePowers.add(sysRolePower);
        });
        int result = rolePowerMapper.batchInsert(rolePowers);
        return result > 0;
    }
}




